<?php

/**
 * login actions.
 *
 * @package    openbonsai
 * @subpackage login
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class loginActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
      $this->form= new loginForm() ;


    #Limpiamos las credenciales y la autenticación.
    if($this->getUser()->isAuthenticated()){
	$this->getUser()->setAuthenticated(false);
	$this->getUser()->clearCredentials();
        }

      if($request->isMethod('post')){
          $this->form->bind($request->getParameter('login'));

          if($this->form->isValid()){
              $valores= $this->form->getValues();

              //Seleccionar el password.
              $q= Doctrine_Query::create()
              ->from('users u')
                      ->where('email = ?', $valores['email']);
              $passw= $q->fetchOne();

              //Comprobar.
              if($passw['password']== md5($valores['password'])){

                  //Registrar hora del login.
                  $qHora= Doctrine_Query::create()
                  ->update('users u')
                  ->set('u.last_login', '?', date('Y-m-d H:i:s'))
                  ->where('u.id= ?', $passw->getId())
                  ->execute();

                  //Atributos del usuario.
                  $this->getUser()->setAttribute('usuario', $passw->getNombre());

                  //Poner autenticado y redirección.
                  $this->getUser()->setAuthenticated(true);
                  $this->redirect('@homepage');
              } else{
                  $this->getUser()->setFlash('errorLogin', 'Los datos introducidos no son correctos.');
              }
          }              
              
      }
  }
}
